<template>
	<view class="main-page" style="background: #1E1F27;">
		<view class="title-main" :class="[hasFlow ? 'title-box-flow' : 'title-box-default']">
			<view class="title-container" >
				<image class="back-ic" src="../../static/ic_back_white.png" @click="goBack"></image>
				<text class="font-fff-36 font-weight-600">最星城VIP会员中心</text>
			</view>
		</view>
		
		
		<view class="non-vip-header" v-if="!isVip">
			<image class="head-img" :src="notVipBg"></image>
			<view class="non-vip-info-box">
				<image class="non-vip-avatar" v-if="userInfo.headImg" 
				:src="userInfo.headImg"></image>
				<image class="non-vip-avatar" src="../../static/ic_avatar_profile.png" v-else></image>
				<text class="font-959-24 font-weight-600">{{userInfo.nickName}}</text>
				<view class="non-vip-divider"></view>
				<text class="font-dc9-24">未开通会员</text>
			</view>
		</view>
		
		<view v-if="isVip" class="vip-header">
			<image class="head-img" :src="vipBg"></image>
			<view class="vip-info-box">
				<image class="vip-info-bg" :src="vipInfoBg"></image>
				<view class="vip-avatar-box">
					<image class="vip-avatar" :src="userInfo.headImg" v-if="userInfo.headImg"></image>
					<image class="vip-avatar" src="../../static/ic_avatar_profile.png" v-else></image>
					
					<view class="column-container">
						<view class="row-container">
							<text class="vip-name">{{userInfo.nickName}}</text>
							<image class="vip-icon" src="../../static/ic_vip.png"></image>
						</view>
						<text class="font-b08-22 mt-2">有效期至：{{userInfo.vipTime}}</text>
					</view>
				</view>
				<view class="row-container-row-between ml-24 mr-30 mb-26" style="z-index: 110;">
					<view class="row-container">
						<image class="vip-rights" src="../../static/ic_vip_right.png"></image>
						<image class="vip-rights" src="../../static/ic_only_vip.png"></image>
					</view>
					<!-- <text class="font-9d6-22">会员编号：20230515876</text> -->
				</view>
				
				
			</view>
		</view>
		
		<view class="content-box">
			<view class="row-container-center">
				<image class="title-icon" src="../../static/ic_title_left.png"></image>
				<text class="font-f4c-32 font-weight-600">{{isVip ? '立即续费' : '立即开通'}}</text>
				<image class="title-icon" src="../../static/ic_title_right.png"></image>
			</view>
			<view class="package-list-box">
				<view class="package-item" v-for="(item , index) in packageList" :key="index" 
				@click="changeChosenPackage(item)">
					<image class="package-item-bg" src="../../static/bg_package_item_chosen.png" 
					v-if="item.id == chosenPackageId"></image>
					<image class="package-item-bg" src="../../static/bg_package_item.png"
					v-else></image>
					<text :class="[item.id == chosenPackageId ? 'package-name-chosen' : 'package-name']">{{item.name}}</text>
					<view class="column-container-center" style="z-index: 100;">
						<view class="row-container">
							<text :class="[item.id == chosenPackageId ? 'package-unit-chosen' : 'package-unit']">￥</text>
							<text :class="[item.id == chosenPackageId ? 'package-price-chosen' : 'package-price']">{{item.price}}</text>
						</view>
						<text :class="[item.id == chosenPackageId ? 'package-former-chosen' : 'package-former']">¥ {{item.oldPrice}}</text>
					</view>
					<text :class="[item.id == chosenPackageId ? 'package-days-chosen' : 'package-days']">VIP会员{{item.useDay}}天</text>
					
				</view>
			</view>
			
			<view class="buy-vip-btn" @click="handleClickBuy">
				<image class="buy-bg" src="../../static/bg_btn_buy_vip.png"></image>
				<text class="font-552-36 font-weight-600" style="z-index: 100;">￥{{chosenpackagePrice}}</text>
				<text class="font-552-36 font-weight-600 ml-16" style="z-index: 100;" >
				{{isVip ? '立即续费' : '立即开通'}}</text>
			</view>
			
			<view class="row-container" @click="hasAgree = !hasAgree">
				<image class="check-flag" src="../../static/ic_vip_agreement_default.png" v-if="!hasAgree"></image>
				<image class="check-flag" src="../../static/ic_vip_agreement_chosen.png" v-if="hasAgree"></image>
				<text class="font-727-24">购买即同意</text>
				<text class="font-dc9-24" @click.stop="gotoAgreement">《最星城用户条款》</text>
			</view>
			
			<view class="row-container-center mt-69">
				<image class="title-icon" src="../../static/ic_title_left.png"></image>
				<text class="font-f4c-32 font-weight-600">最星城会员权益说明</text>
				<image class="title-icon" src="../../static/ic_title_right.png"></image>
			</view>
			
			<view class="explain-box">
				<view class="title-box">
					<text class="font-727-28">VIP会员可额外获得至少</text>
					<text class="font-dc9-28">2元</text>
					<text class="font-727-28">的返现金额，根据订单满返比例不同，每单提升</text>
					<text class="font-dc9-28">2元~5元</text>
					<text class="font-727-28">不等</text>
				</view>
				<text class="hint-text">*额外返现只对会员生效后的订单生效，购买会员前的订单无法额外获得金额提升哦！</text>
			</view>
			
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo:{},
				packageList:[],
				chosenPackageId : -1, // 选择的套餐id
				chosenpackagePrice: '0' , // 选择的套餐价格
				hasAgree:false,
				hasFlow:false,
				isVip:false , 
				
				canClick:true , //是否可点击下单
				
				notVipBg:'https://jxshoss.oss-cn-qingdao.aliyuncs.com/common/20240603122503.png' , // 
				vipBg:'https://jxshoss.oss-cn-qingdao.aliyuncs.com/common/20240603122510.png',
				vipInfoBg:'https://jxshoss.oss-cn-qingdao.aliyuncs.com/common/20240603122453.png'
			}
		},

		onPageScroll(res) {
			if (res.scrollTop >= 10){
				this.hasFlow = true
			} else{
				this.hasFlow = false
			}
		},
		
		onLoad() {
			this.loadUserDetail()
			this.loadPackageList()
		},
		
		methods: {
			goBack(){
				uni.navigateBack()
			},
			
			gotoAgreement(){
				uni.navigateTo({
					url:'/pages/mine/agreement'
				})
			},
			
			loadUserDetail(){
				let that = this
				this.http({
					url:"/user/userCenter",
					method:"POST",
					hideLoading:true,
					data:{},
					success(data){
						if (data.code == 200){
							that.userInfo = data.data
							that.isVip = data.data.checkVip == 1
							that.cookie.set("jx_nickname" , data.data.nickName)
							that.cookie.set("jx_avatar" , data.data.headImg)
							that.cookie.set("jx_mobile" , data.data.mobile)
						}
					},
					failed(e){
					}
				})
			},
			
			loadPackageList(){
				let that = this
				this.http({
					url:"/user/vip/package/list",
					method:"POST",
					data:{},
					success(data){
						if (data.code == 200){
							that.packageList = data.data
							if (data.data.length > 0){
								that.chosenPackageId = data.data[0].id
								that.chosenpackagePrice = data.data[0].price
							}
						}
					},
					failed(e){
					}
				})
			},
			
			changeChosenPackage(item){
				this.chosenPackageId = item.id
				this.chosenpackagePrice = item.price
			},
			
			//点击立即开通或者立即续费
			handleClickBuy(){
				if (!this.canClick){
					return
				}
				
				if (!this.chosenPackageId) {
					uni.showToast({
						title:'请选择开通的套餐',
						icon:'none'
					})
					return
				}
				if (!this.hasAgree){
					uni.showToast({
						title:'请阅读并同意《最星城VIP权益条款》',
						icon:'none'
					})
					return
				}
				
				this.canClick = false
				
				let that = this
				this.http({
					url:"/user/vip/order/createOrder",
					method:"POST",
					data:{
						tcId : that.chosenPackageId
					},
					success(res){
						if (res.code == 200){
							let orderNo = res.data.orderNo
							that.handleCommitOrder(orderNo)
						} else{
							that.canClick = true
						}
					},
					failed(e){
						that.canClick = true
					}
				})
			},
			
			//统一下单
			handleCommitOrder(orderNo){
				if (!orderNo){
					this.canClick = true
					uni.showToast({
						title:"订单id为空",
						icon:'none'
					})
					return
				}
				let that = this
				this.http({
					url:'/pay/unifiedOrder',
					method:"POST",
					data:{
						outTradeNo:orderNo
					},
					header : {
						"content-type": "application/json",
						'Authorization': uni.getStorageSync("jx_token")
					},
					success(res){
						if (res.code == 200){
							that.payOrder(res.data)							
						} else{
							this.canClick = true
							setTimeout(()=>{
								uni.showToast({
									title:res.msg,
									icon:'none'
								})
							} , 500)
						}
						
					},
					failed(e){
						this.canClick = true
					}
				})
			},
			
			//调用支付
			payOrder(payData){
				let that = this
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: payData.timeStamp,
					nonceStr: payData.nonceStr,
					package: payData.package,
					signType: payData.signType,
					paySign: payData.paySign,
					success(res) {
						that.canClick = true
						uni.showToast({
							title:'支付成功',
							icon:'none'
						})
						that.loadUserDetail()
					},
					fail(e) {
						that.canClick = true
						console.log('payOrder-----' , e)
					}
				})
			},
			
		}
	}
</script>

<style lang="scss">
	.title-main{
		display: flex;
		flex-direction: column;
		width: 100%;
		position: fixed;
		z-index: 10000;
	}
	.title-container{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 88rpx;
		margin-top: 88rpx;
		
		.back-ic{
			width: 64rpx;
			height: 64rpx;
			padding: 0rpx 22rpx;
			position: absolute;
			left: 0rpx;
		}
	}
	
	.title-box-default{
		background: transparent;
	}
	
	.title-box-flow{
		background: #1E1F27;
	}
	
	.non-vip-header{
		width: 100%;
		height: 540rpx;
		display: flex;
		flex-direction: column;
		position: relative;
		
		.non-vip-info-box{
			display: flex;
			flex-direction: row;
			align-items: center;
			z-index: 100;
			margin-left: 30rpx;
			margin-top: 206rpx;
			
			.non-vip-avatar{
				width: 44rpx;
				height: 44rpx;
				margin-right: 12rpx;
				border: white solid 2rpx;
				border-radius: 22rpx;
			}
			
			.non-vip-divider{
				width: 2rpx;
				height: 20rpx;
				margin-left: 10rpx;
				margin-right: 10rpx;
				background: #494C58;
			}
		}
	}
	
	.vip-header{
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: flex-end;
		width: 100%;
		height: 505rpx;
		position: relative;
		
		.vip-info-box{
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			width: calc(100% - 60rpx);
			height: 305rpx;
			position: relative;
			z-index: 100;
			
			.vip-info-bg{
				width: 100%;
				height: 100%;
				position: absolute;
			}
			
			.vip-avatar-box{
				display: flex;
				flex-direction: row;
				align-items: center;
				z-index: 110;
				margin-top: 73rpx;
				margin-left: 24rpx;
				
				.vip-avatar{
					width: 80rpx;
					height: 80rpx;
					margin-right: 16rpx;
					border-radius: 40rpx;
				}
				
				.vip-name{
					color: #72360B;
					font-size: 32rpx;
					font-weight: 600;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
					display: inline;
					max-width: 6em;
					margin-right: 6rpx;
					line-height: 44rpx;
				}
				
				.vip-icon{
					width: 78rpx;
					height: 26rpx;
				}
				
			}
			
			.vip-rights{
				width: 130rpx;
				height: 36rpx;
				margin-right: 19rpx;
				
			}
			
			
		}
	}
	
	.head-img{
		width: 100%;
		height: 100%;
		position: absolute;
	}
	
	.content-box{
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		flex-grow: 1;
		background: #252732;
		border-top-left-radius: 25rpx;
		border-top-right-radius: 25rpx;
		padding: 40rpx 0rpx 45rpx 0rpx;
		
		.title-icon{
			width: 89rpx;
			height: 26rpx;
			margin: 0rpx 20rpx;
		}
		
		.package-list-box{
			display: flex;
			flex-direction: row;
			width: calc(100% - 36rpx);
			margin: 49rpx 18rpx 0rpx 18rpx;
			overflow-x: auto;
			overflow-y: hidden;
			
			.package-item{
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: space-between;
				width: 214rpx;
				height: 264rpx;
				margin: 0rpx 12rpx 40rpx 12rpx;
				position: relative;
				flex-shrink: 0;
				
				.package-item-bg{
					width: 100%;
					height: 100%;
					position: absolute;
				}
				
				.package-name-chosen{
					color: #825F4E;
					font-size: 28rpx;
					font-weight: 600;
					z-index: 100;
					margin-top: 8rpx;
				}
				
				.package-name{
					color: #CCB9A4;
					font-size: 28rpx;
					z-index: 100;
					margin-top: 8rpx;
				}
				
				.package-unit-chosen{
					color: #552B17;
					font-size: 24rpx;
					margin-top: 18rpx;
				}
				
				.package-unit{
					color: #CCB9A4;
					font-size: 24rpx;
					margin-top: 18rpx;
				}
				
				.package-price-chosen{
					color: #552B17;
					font-size: 44rpx;
					font-weight: 700;
				}
				
				.package-price{
					color: #CCB9A4;
					font-size: 44rpx;
					font-weight: 700;
				}
				
				.package-former-chosen{
					color: #A68677;
					font-size: 24rpx;
					margin-top: 2rpx;
					text-decoration: line-through;
				}
				
				.package-former{
					color: #585A68;
					font-size: 24rpx;
					margin-top: 2rpx;
					text-decoration: line-through;
				}
				
				.package-days-chosen{
					color: #6A4533;
					font-size: 26rpx;
					margin-bottom: 23rpx;
					z-index: 100;
				}
				
				.package-days{
					color: #727481;
					font-size: 26rpx;
					margin-bottom: 23rpx;
					z-index: 100;
				}
			}
		}
		
		.buy-vip-btn{
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;
			width: calc(100% - 60rpx);
			height: 96rpx;
			margin: 0rpx 30rpx 30rpx 30rpx;
			position: relative;
			
			.buy-bg{
				width: 100%;
				height: 100%;
				position: absolute;
			}
		}
		
		.check-flag{
			width: 22rpx;
			height: 22rpx;
			margin-right: 9rpx;
		}
		
		.explain-box{
			display: flex;
			flex-direction: column;
			width: calc(100% - 108rpx);
			margin: 32rpx 30rpx 0rpx 30rpx;
			padding: 24rpx;
			background: #2B2B37;
			border: 2rpx solid #B69282;
			border-radius: 16rpx;
			
			.title-box{
				display: inline;
			}
			
			.hint-text{
				width: calc(100% - 48rpx);
				padding: 14rpx 24rpx;
				background: #262631;
				margin-top: 12rpx;
				color: #97766B;
				font-size: 24rpx;
				line-height: 33rpx;
			}
		}
		
	}
	
	
</style>
